package com.reebake.ideal.permission.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.reebake.ideal.permission.entity.Menu;
import com.reebake.ideal.permission.service.impl.MenuServiceImpl;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

@Service
@Primary
public class MenuServicePlusImpl extends MenuServiceImpl {
    public static final String ROOT_ID = "0";

    private void notLeaf(String menuId){
        LambdaUpdateWrapper<Menu> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Menu::getIsLeaf, false).eq(Menu::getMenuId, menuId).eq(Menu::getIsLeaf, true);
        super.update(updateWrapper);
    }

    @Override
    public boolean saveOrUpdate(Menu entity) {
        String parentId = entity.getParentId();

        boolean result = super.saveOrUpdate(entity);
        if(result && !ROOT_ID.equals(parentId)) {
            notLeaf(parentId);
        }
        return result;
    }
}
